2111c0
@@ -207,8 +207,10 @@
private int createPermanentFunction(Hive db, CreateFunctionDesc createFunctionDe
     try {
       db.createFunction(func);
     } catch (Exception e) {
-      // Addition to metastore failed, remove the function from the registry.
-      FunctionRegistry.unregisterPermanentFunction(registeredName);
+      // Addition to metastore failed, remove the function from the registry except if already exists.
+      if(!(e.getCause() instanceof AlreadyExistsException)) {
+        FunctionRegistry.unregisterPermanentFunction(registeredName);
+      }
       setException(e);
       LOG.error("Failed to add function " + createFunctionDesc.getFunctionName() +
               " to the metastore.", e);
